import 'package:android_play_install_referrer/android_play_install_referrer.dart';
import 'package:flutter/services.dart';
import 'package:liuxlcommon/util/print_utils.dart';
import 'package:ladder_cash/app/com/http/http_request.dart';
import 'package:ladder_cash/app/key_mix.dart';

class GoogleMarkerPlugin {
  static const MethodChannel _channel = MethodChannel('GoogleMarketPlugin');
  static String? adid;

  static Future<String> get getADID async {
    return adid ??= await _channel.invokeMethod("getADID") ?? "";
  }

  static Future<bool> initAdjust(material) async {
    return await _channel.invokeMethod("initAdjust", {'material': material});
  }

  static init() async {
    List l = await Future.wait([installReferrer(), getADID]);
    var referrer = l[0];
    String adid = l[1];
    // String? gaid = await AdvertisingId.id();
    // ToastUtils.toast(adid);
    var data = await HttpRequest().post(
        '/v3/loan-confirm/google-market'.hx, {'gps_adid'.hx: adid, 'market'.hx: referrer},
        autoToast: false);
    if (data != null) {
      // bool b = await initAdjust(data['afkey'.hx]);
      // PrintUtils.p("initAdjust: $b");
    }
  }

  static Future<String?> installReferrer() async {
    try {
      return (await AndroidPlayInstallReferrer.installReferrer).installReferrer;
    } catch (e) {
      return "";
    }
  }
}
